;(function () {
    'use strict';
    angular.module('user',[])
        .service('UserService',[
            '$state',
            '$http',
            function ($state,$http) {
                var me = this;
                me.signup_data = {};
                me.login_data = {};
                me.signup = function () {
                    $http.post('/api/user/signup',
                        {username:me.signup_data.username,
                            password:me.signup_data.password
                        })
                        .then(function (r) {
                            if (r.data.status){
                                me.signup_data = {};
                                $state.go('login');
                            }
                        })
                }
                me.username_exists = function () {
                    $http.post('/api/user/exist',
                        {username:me.signup_data.username})
                        .then(function (r) {
                            if (r.data.status)
                                me.signup_username_exists=false
                            else me.signup_username_exists = true
                        },function (e) {
                            console.log('e',e);
                        })
                }

                me.login = function () {
                    $http.post('/api/user/login'
                        ,me.login_data)
                        .then(function (r) {
                            if (r.data.status)
                            {
                                me.login_data = {};
                                location.href = '/';
                            }
                            else{
                                me.login_err = true;
                            }

                        },function () {
                            console.log('r',r);
                        })
                }
                me.login_reset = function () {
                    me.login_err  = false;
                }
            }
        ])

        .service('LogoutUserService',[
            '$state',
            '$http',
            function ($state,$http) {
                var me = this
                me.logout = function () {
                    $http.post('/api/user/logout')
                        .then(function () {
                            // $state.go('home');
                            location.href='/';
                        },function () {

                        })
                }
            }
        ])
        .controller('LogoutUserController',[
            '$scope',
            'LogoutUserService',
            function ($scope,LogoutUserService) {
                $scope.LogoutUser = LogoutUserService;
            }
        ])

        .controller('SignupController',[
            '$scope',
            'UserService',
            function ($scope,UserService) {
                $scope.User = UserService;
                $scope.$watch(function () {
                    return UserService.signup_data;
                },function (n,o) {
                    if (n.username != o.username){
                        UserService.username_exists();
                    }
                },true)
            }
        ])
        .controller('LoginController',[
            '$scope',
            'UserService',
            function ($scope,UserService) {
                $scope.User = UserService;
            }
        ])
})();